﻿using B.Auth.APi.Application.Command.Menus;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Menuss
{
    public class DelMenuCommandHand : IRequestHandler<DelMenuCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<MenuModel> _menu;

        public DelMenuCommandHand(IBaseRepository<MenuModel> menu)
        {
            _menu = menu;
        }

        public Task<ApiResult<int>> Handle(DelMenuCommand request, CancellationToken cancellationToken)
        {

            var result = new ApiResult<int>();

            var menu = new MenuModel();
            menu.Id = request.Id;
            menu.MenuName = request.MenuName;
            menu.Url = request.Url;
            menu.PID = request.PID;

            
            menu.MenuDel = 1;
            Del(request.Id);
            _menu.Update(menu);
            result.Code = ApiEnum.ok;
            result.Message = "删除成功";
            return Task.FromResult(result);


        }

        public string Del(int id)
        {

            var m = _menu.GetAll().Where(x => x.PID == id).ToList();
            if (m != null)
            {

                foreach (var item in m)
                {

                    item.MenuDel = 1;
                    Del(item.Id);

                }
            }
            return "删除成功";
        
        }



        }
}
